//
//  MainViewController.swift
//  我的微博
//
//  Created by 原 on 15/10/8.
//  Copyright © 2015年 原哥. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
    
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        setupComposedButton()
    }
    
    
    /// 添加所有控制器
    private func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
            
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
            
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    
    
}
    
    /// 设置按钮
    
    
    /// 添加子控制器
    //属性的由来 参数  private????
    private func addChildViewController(vc: UIViewController, title: String , imageName: String) {
        
                // title
        // image
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        
        // 根控制器
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
        
    }
    //注意括号的范围!!!!
    // MARK: - 懒加载控件
    lazy private var composeButton:UIButton = {
        
        // 定义安钮
        let button = UIButton()
        // 设置按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //添加按钮
        self.tabBar.addSubview(button)
        // 按钮添加点击事件
        button.addTarget(self, action: "clickComposedButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        }()
    
    /// 按钮点击
    func clickComposedButton() {
        
        print(__FUNCTION__)
        
    }
    // 设置按钮位置
    private func setupComposedButton() {
        
        let w = tabBar.bounds.width / CGFloat(viewControllers!.count)
        
        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        
        composeButton.frame = CGRectOffset(rect, 2*w, 0)
        
    }
    

}
